/**
 * ReportGear(2011)
 */
package com.reportgear.report.util;

/**
 * 报表实用类
 * 
 * @version 1.0 2011-5-8
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ReportUtils {
	// 字母总数
	private static final int LETTER_NUM = 26;
	// 字母表
	private static final char[] digits = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
			'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

	/**
	 * 转换整数到字母代码 1-A,2-B
	 * 
	 * @param index
	 *            所有
	 * @return 转换后字符串
	 */
	public static String convertIntToABC(int index) {
		int pos = index;
		StringBuilder sb = new StringBuilder();

		if (pos == 0) {
			return "";
		}

		while (pos != 0) {
			int inPos = pos % LETTER_NUM;
			if (inPos == 0) {
				inPos = LETTER_NUM;
			}

			sb.insert(0, digits[(inPos - 1)]);

			pos = (pos - inPos) / LETTER_NUM;
		}

		return sb.toString();
	}

	/**
	 * ABC字符串转化成整型值
	 * 
	 * @param absStr
	 *            abc字符串
	 * @return 代表的整数
	 */
	public static int convertABCToInt(String absStr) {
		int ret = 0;
		absStr = absStr.toUpperCase();

		for (int i = 0, len = absStr.length(); i < len; ++i) {
			ret = Character.getNumericValue(absStr.charAt(i)) - Character.getNumericValue('A') + LETTER_NUM * ret + 1;
		}

		return ret;
	}

}
